package com.example.onenetdht11;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.onenetdht11.entity.User2;
import com.example.onenetdht11.util.OneNetUtils;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

@RequiresApi(api = Build.VERSION_CODES.O)
public class MainActivity extends AppCompatActivity {

    LinearLayout lledit, lllist, llmy, llset;
    ImageButton ibedit, iblist, ibmy, ibset;
    ViewPager vpContent;
    public static  MainActivity intance = null;
    private OneNetUtils utils;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();
        initData();
        intance = this;
        Log.i("TAG", "onCreate: ");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        initView();
        initListener();
        initData();
        Log.i("TAG", "onRestart: ");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 1){
            System.out.println("result");
            initView();
            initListener();
            initData();

        }
    }

    void initView() {
        lledit = findViewById(R.id.table_edit);
        lllist = findViewById(R.id.table_list);
        llmy = findViewById(R.id.table_my);
//        llset = findViewById(R.id.table_set);

        ibedit = findViewById(R.id.iv_edit);
        iblist = findViewById(R.id.iv_list);
        ibmy = findViewById(R.id.iv_my);
//        ibset = findViewById(R.id.iv_set);

        vpContent = findViewById(R.id.vp_content);

    }

    View.OnClickListener listener = new View.OnClickListener() {
        @SuppressLint("NonConstantResourceId")
        @Override

        public void onClick(View view) {
            resetImages();
            switch (view.getId()) {
                case R.id.table_edit:
                    selectTab(Contests.TABEDIT);
                    break;
                case R.id.table_list:
                    selectTab(Contests.TABLIST);
                    break;
/*                case R.id.table_set:
                    selectTab(Contests.TABSET);
                    break;*/
                case R.id.table_my:
                    selectTab(Contests.TABMY);
                    break;

            }
        }
    };

    public void initListener() {

        lledit.setOnClickListener(listener);
//        llset.setOnClickListener(listener);
        lllist.setOnClickListener(listener);
        llmy.setOnClickListener(listener);
    }

    public void resetImages() {
        ibmy.setImageResource(R.mipmap.my2);
//        ibset.setImageResource(R.mipmap.set2);
        iblist.setImageResource(R.mipmap.power1);
        ibedit.setImageResource(R.mipmap.zhuxin);
    }

    public void selectTab(int s) {
        switch (s) {
            case Contests.TABEDIT:
                ibedit.setImageResource(R.mipmap.zhuxing);
                break;
            case Contests.TABLIST:
                iblist.setImageResource(R.mipmap.power2);
                break;
            case Contests.TABMY:
                ibmy.setImageResource(R.mipmap.my1);
                break;
            case Contests.TABSET:
                ibset.setImageResource(R.mipmap.set1);
                break;
        }
        //设置当前点击的TAB所对应的页面
        vpContent.setCurrentItem(s);
    }

    /**
     * 初始化各种数据
     */
    public void initData() {
//        new Thread(runnable).start();


        //获取登录用户的数据
        User2 user = (User2) getIntent().getSerializableExtra("user");
        //
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String weatherId = prefs.getString("weather", null);
        if (weatherId == null) {
            weatherId = getIntent().getStringExtra("weather_id");
        }
        System.out.println("weatherId=" + weatherId);
        Contests.NextCity = weatherId;
        try {
            utils = new OneNetUtils(this);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        //初始化fragment列表，并添加进去
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new MyFragment(this, user, utils));
        fragments.add(new ListFragment(this, user,utils));
        fragments.add(new EditFragment(this));
//        fragments.add(new SetFragment(this, user));
        //初始化适配器，并绑定适配器
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        };
        //绑定滚动事件侦听
        vpContent.setAdapter(adapter);
        vpContent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                resetImages();
                selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

/*        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                Contests.MaxTemp = userDao.getmaxtemp(Contests.id);
                Contests.MinTemp = userDao.getmintemp(Contests.id);
            }
        };*/

    /**
     * 跳转到下一个界面
     * @param c -要跳转界面的类名
     */
    public void gotoNextActivity(Class c){
        Intent intent_back = new Intent();
        intent_back.setClass(this,c);
        startActivity(intent_back);
        finish();
    }

    /**
     * 带着user对象跳转到下一个界面
     * @param c 下一个界面的类对象
     * @param user  要传递的user对象
     */
    public void gotoNextActivity(Class c,User2 user){
        Intent intent_back = new Intent();
        intent_back.setClass(this,c);
        Bundle bundle = new Bundle();
        bundle.putSerializable("user",user);
        intent_back.putExtras(bundle);
        startActivity(intent_back);
//        finish();
    }

}